﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLogic;
using BusinessObjects;
using System.Net.Mail;
using Common;

namespace Presentation {
  /// <summary>
  /// Login form
  /// </summary>
  public partial class LoginForm : Form {

    private UserBll bll = new UserBll( );
    private bool loginSucceeded;

    #region - Properties -

    /// <summary>
    /// Gets or sets a value indicating whether [login succeeded].
    /// </summary>
    /// <value>
    ///   <c>true</c> if [login succeeded]; otherwise, <c>false</c>.
    /// </value>
    public bool LoginSucceeded {
      get { return loginSucceeded; }
      set { loginSucceeded = value; }
    }

    #endregion

    #region - Construtor -

    /// <summary>
    /// Initializes a new instance of the <see cref="LoginForm"/> class.
    /// </summary>
    public LoginForm( ) {
      InitializeComponent( );
    }

    #endregion

    #region - Events -

    private void loginButton_Click( object sender, EventArgs e ) {
      if ( ValidateChildren( ) ) {
        if ( resetCheckBox.Checked ) {
          if ( MailHelper.InternetConnectionAvailable( ) ) {

            try {
              User user = bll.GetUser( nameTextBox.Text );
              user.Password = "Paswoord1";
              user.AutoLogin = false;

              new UserBll( ).SaveUserPassword( user );

              string body = "Loginnaam: " + user.LoginName + "\n"
                + "Paswoord : Paswoord1 \n"
                + "Gelieve zo snel mogelijk te wijzigen!";

              MailHelper.SendMail( SettingsSingleton.Settings.SystemEmail, user.Email, "Nieuw paswoord",
                body, SettingsSingleton.Settings.SmtpServerName, SettingsSingleton.Settings.SmtpPort,
                SettingsSingleton.Settings.SmtpUserName, SettingsSingleton.Settings.SmtpPassword );

              MessageBox.Show( "Uw paswoord is gewijzigd en verzonden naar \n "
                + SettingsSingleton.Settings.CurrentUser.Email + "\n\n"
                + "De applicatie zal herstarten" );
              Application.Restart( );

            } catch ( Exception ex ) {
              MessageBox.Show( ex.Message );
            }
          } else {
            MessageBox.Show( "Geen internetvebinding gevonden. \nHet paswoord is niet vervangen.",
              "Geen verbinding", MessageBoxButtons.OK, MessageBoxIcon.Exclamation );
          }
        } else {
          loginSucceeded = bll.AuthenticateUser( nameTextBox.Text, passwordTextBox.Text );
        }
      }
    }

    #endregion

    #region - Validation -

    private void nameTextBox_Validating( object sender, CancelEventArgs e ) {
      if ( this.DialogResult != DialogResult.None ) return;
      extendedErrorProvider.SetError( nameTextBox, "" );
      if ( string.IsNullOrEmpty( nameTextBox.Text ) ) {
        extendedErrorProvider.SetError( nameTextBox, "Loginnaam verplicht." );
        e.Cancel = true;
      }
    }

    private void passwordTextBox_Validating( object sender, CancelEventArgs e ) {
      if ( this.DialogResult != DialogResult.None ) return;
      if ( !resetCheckBox.Checked ) {
        extendedErrorProvider.SetError( passwordTextBox, "" );
        if ( string.IsNullOrEmpty( passwordTextBox.Text ) ) {
          extendedErrorProvider.SetError( passwordTextBox, "Paswoord verplicht." );
          e.Cancel = true;
        }
      }
    }

    #endregion

    #region - Escape the form -

    protected override void OnFormClosing( FormClosingEventArgs e ) {
      base.OnFormClosing( e );
      e.Cancel = false;
    }

    protected override bool ProcessDialogKey( Keys keyData ) {
      if ( keyData == Keys.Escape ) this.DialogResult = DialogResult.Cancel;
      return base.ProcessDialogKey( keyData );
    }

    #endregion

  }
}
